"use strict";
const mvc = require("cosjs.mvc");
const promise = mvc.library.require("promise");
//this=updater;
module.exports = async function (tar, key, STime, ETime) {

    let mainKey = key;
    let subKey = -1;
    if (key.indexOf('.') != -1) {
        let ss = key.split('.');
        mainKey = ss[0];
        subKey = ss[1];
    }


    if (mainKey == 'hasCard') {
        let cardId = parseInt(subKey);
        let _que = { 'uid': this.uid, 'bag': 50, 'id': cardId };
        let _opt = {};
        let ret = await this.item.model.find(_que, _opt);
        if (ret) {
            return 1;
        } else {
            return 0;
        }
    }




    let keyType;
    switch (tar) {
        case 1:
            {
                let format = mvc.format('role')[mainKey];
                if (format) {
                    keyType = format.type;
                } else {
                    console.log('role', mainKey, 'format not exist');
                }
            }
            break;
        case 2:
            //成就数据
            break;
        case 3:
            //图鉴数据
            break;
        case 10:
            break;
        case 20:
        case 100:
            {
                let format = mvc.format('daily')[mainKey];
                if (format) {
                    keyType = format.type;
                } else {
                    console.log('daily', mainKey, 'format not exist');
                }
            }
            break;
        case 101:
            {
                let format = mvc.format('daily')[mainKey];
                keyType == 'array'
                // if (format) {
                //     keyType = format.type;
                // } else {
                //     console.log('daily', mainKey, 'format not exist');
                // }
            }
            break;
    }
    if (keyType == 'array') {
        key = mainKey;
    }

    let ret;




    let Pv;
    if (tar === 1) {  //玩家数据
        //   Pv = this.role.model.uget(key);
        let _que = { '_id': this.uid };
        let _opt = {};
        _opt['fields'] = [key];
        ret = await this.role.model.find(_que, _opt);
        if ((keyType == 'array') && (subKey != -1)) {
            ret = ret[mainKey][subKey];
        }
    }
    else if (tar === 2 || tar === 4) { //成就
        ret = await this.record.model.get(key, 'val');
    }
    else if (tar === 3) { //图鉴

        //mainKey/100 == 1   角色
        //mainKey/100 == 2   芯片
        if (mainKey >= 100 && mainKey < 199) {
            //角色
            let retData = await this.collect.model.mget(1);
            let collectType = mainKey % 100;
            ret = 0
            if (collectType == 0) {
                ret = retData.length;
            } else {
                for (let i in retData) {
                    if (parseInt((retData[i].id % 100000) / 1000) == collectType) {
                        ret++
                    }
                }
            }
        }
        else if (mainKey == 199) {
            //敌人
            ret = await this.collect.model.mget(3);
        }
        else if (mainKey >= 200 && mainKey < 300) {
            //芯片
            // ret = await this.collect.model.mget(2);
            retData = await this.collect.model.mget(2);
            collectType = mainKey % 100;
            ret = 0
            for (let i in retData) {
                if (parseInt((retData[i].id % 100000) / 1000) == collectType) {
                    ret++
                }
            }
        }
        else {
            //物品
            ret = await this.collect.model.mget(3);
        }
        //需要对数据做额外处理
    }
    else if (tar === 10) {//每日数据
        let _que = { 'uid': this.uid };
        _que['time'] = mvc.library("time/sign");
        let _opt = {};
        _opt['fields'] = [key];
        ret = await this.daily.model.find(_que, _opt);
        if ((keyType == 'array') && (subKey != -1)) {
            ret = ret[mainKey][subKey];
        }
    }
    else if (tar === 20) {//每周数据，本周
        if (keyType == 'array') {
            ret = await this.daily.model.weekArray(mainKey, subKey);
        } else {
            ret = await this.daily.model.week(key);
        }

    }
    else if (tar === 100) {//活动期间日常数据累计
        if (keyType == 'array') {
            ret = await this.daily.model.sumArray(STime, ETime, mainKey, subKey);
        } else {
            ret = await this.daily.model.sum(STime, ETime, key);
        }
    }
    else if (tar === 101) {//活动期间日常数据最高
        // if (keyType == 'array') {
        ret = await this.daily.model.sumMax(STime, ETime, mainKey, subKey);
        // } else {
        //     ret = await this.daily.model.sumMax(STime, ETime, key);
        // }
    }
    else {//0：固定为1，不需要条件，其他（无法识别）固定为0，无法完成
        ret = tar ? 0 : 1;
    }

    let val;
    if (typeof ret === 'number') {
        val = ret;
    }
    else {
        val = obj_getter_val(ret, key);
    }
    return val;

};





function obj_getter_val(val, key) {
    if (!val || typeof val !== 'object') {
        return 0;
    }
    if (val[key]) {
        return val[key];
    }
    if (String(key).indexOf(".") < 0) {
        return val[key] || 0
    }
    let d = obj_select_key(val, key);
    if (!d) {
        return 0;
    }
    let k = d["key"];
    return d["val"][k] || 0;
}



function obj_select_key(value, key) {
    let result = { "key": key, "val": value };
    if (key.indexOf(".") > 0) {
        let arr = key.split(".");
        result["key"] = arr.pop();
        for (let k of arr) {
            if (!(k in result["val"])) {
                result["val"][k] = {};
            }
            if (!result["val"][k] || typeof result["val"][k] !== 'object') {
                return false;
            }
            result["val"] = result["val"][k];
        }
    }
    if (arguments.length > 2 && !(result["key"] in result["val"])) {
        result["val"][result["key"]] = arguments[2];
    }
    return result;
}